package com.loongxun.playwithyou.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.loongxun.playwithyou.ApiInterface;
import com.loongxun.playwithyou.App;
import com.loongxun.playwithyou.R;
import com.loongxun.playwithyou.bean.Member;
import com.loongxun.playwithyou.bean.SendRequest;
import com.loongxun.playwithyou.network.MyHttpClientRespenseHandler;
import com.loongxun.playwithyou.network.nHttpClient;
import com.loongxun.playwithyou.ui.adapter.MemberListAdapter;
import com.loongxun.playwithyou.util.ToastView;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 下单页
 * Created by Administrator on 2017/9/21.
 */
public class SaveOrderActivity extends Activity implements View.OnClickListener{

    private Context context;
    private TextView tv_top_title,tv_baspeak_data_time,tv_baspeak_hours_num,tv_hint,
            tv_coach_name,tv_coach_dan,tv_coach_heat,tv_to_save_order;
    private ImageView iv_coach_img;

    private SendRequest send;

    //用户编辑器
    private SharedPreferences share;
    private SharedPreferences.Editor editor;
    //预约时间,陪玩时长,
    private String playStartTime = "",hoursNum = "",userId = "";
    private Member member;
    //订单号，描述，价格
    private String orderNo,subject,price;

    private ImageLoader imageLoader = ImageLoader.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_bespeak);
        context = SaveOrderActivity.this;

        share = context.getSharedPreferences("userInfo", 0);
        editor = share.edit();
        userId = share.getString("user_id", "");
        member = (Member) getIntent().getSerializableExtra("member");
        playStartTime = getIntent().getStringExtra("playStartTime");
        hoursNum = getIntent().getStringExtra("hoursNum");

        initView();
    }

    private void initView() {
        tv_top_title = (TextView) findViewById(R.id.tv_top_title);
        tv_top_title.setText("预约");
        findViewById(R.id.iv_back).setOnClickListener(this);

        tv_baspeak_data_time = (TextView) findViewById(R.id.tv_baspeak_data_time);
        tv_baspeak_hours_num = (TextView) findViewById(R.id.tv_baspeak_hours_num);
        tv_hint = (TextView) findViewById(R.id.tv_hint);
        tv_baspeak_data_time.setText(playStartTime);
        tv_baspeak_hours_num.setText(hoursNum+"小时");
        tv_hint.setText("您选择的教练为");

        findViewById(R.id.ll_choice).setVisibility(View.GONE);
        findViewById(R.id.ll_coach).setVisibility(View.VISIBLE);

        iv_coach_img = (ImageView) findViewById(R.id.iv_coach_img);
        tv_coach_name = (TextView) findViewById(R.id.tv_coach_name);
        tv_coach_dan = (TextView) findViewById(R.id.tv_coach_dan);
        tv_coach_heat = (TextView) findViewById(R.id.tv_coach_heat);

        tv_coach_name.setText(member.getMember_name());
        tv_coach_dan.setText("平台预约教练");
        tv_coach_heat.setText(member.getClick_num()+"");
        imageLoader.displayImage(ApiInterface.IMAGES + member.getMember_img(), iv_coach_img, App.options);

        tv_to_save_order = (TextView) findViewById(R.id.tv_to_save_order);
        tv_to_save_order.setVisibility(View.VISIBLE);
        tv_to_save_order.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_to_save_order:
                initData();
                break;
            default:
                break;
        }
    }

    private void initData() {
        try {
            send = new SendRequest();
            send.playStartTime = playStartTime;
            send.hoursNum = hoursNum;
            send.memberId = member.getMember_id()+"";
            send.userId = userId;
            //下单
            nHttpClient.getAll(ApiInterface.SAVE_ORDER, send.tofindSaveOrderJson().toString(), indexHandler);
        }catch (Exception e){
            ToastView toast = new ToastView(context, "参数错误");
            toast.show();
        }
    }

    private void toPlayActivity(){
        Intent intent = new Intent(context, ToPlayAcitvity.class);
        intent.putExtra("orderNo",orderNo);
        intent.putExtra("subject",subject);
        intent.putExtra("price",price);
        startActivity(intent);
        finish();
    }

    private MyHttpClientRespenseHandler indexHandler = new MyHttpClientRespenseHandler() {

        @Override
        public void success(JSONObject json) {
            Integer succeed = json.optJSONObject("status").optInt("succeed", 0);
            if (succeed == 1) {
                try {
                    try {
                        JSONObject jsonObject = json.optJSONObject("data").getJSONObject("order");
                        orderNo = jsonObject.getString("order_no");
                        subject = "";
                        price = jsonObject.getString("order_all_price");
                        toPlayActivity();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }else {
                String error_desc = json.optJSONObject("status").optString("error_desc");
                Toast.makeText(context, error_desc, Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void faile(String json) {
            Toast.makeText(context, json, Toast.LENGTH_SHORT).show();
        }
    };

}
